<template>
  <div class="login-wrap">
    <div class="ms-title" style="color: black">欢迎登陆</div>
    <div class="ms-login">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">

        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" placeholder="密码" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  // 2. 注册 identify 组件
  data () {
    return {
      identifyCodes: '1234567890',
      identifyCode: '',
      errorInfo: false,
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入帐号', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    submitForm () {
      this.$axios.post('http://localhost:8001/login', this.ruleForm)
        .then(response => {
          if (response.data.flag === 200) {
            sessionStorage.setItem('id', response.data.log.id)
            sessionStorage.setItem('username', response.data.log.username)
            sessionStorage.setItem('password', response.data.log.password)
            this.$router.replace({path: '/index'})
          } else {
            alert('账户名或密码错误')
            location.reload()
          }
        })
    }
  },

  gotoRegister () {
    this.$router.push('/Register')
  },
  randomNum (min, max) {
    return Math.floor(Math.random() * (max - min) + min)
  }
}
</script>

<style scoped>
.login-wrap {
  height: 100%;
  width: 100%;
  background-size: cover;
  position: fixed;
}

.ms-title {
  position: absolute;
  top: 50%;
  width: 100%;
  margin-top: -230px;
  text-align: center;
  font-size: 30px;
  color: #fff;

}

.ms-login {
  position: absolute;
  left: 50%;
  top: 45%;
  width: 400px;
  height: 240px;
  margin: -150px 0 0 -190px;
  padding: 40px;
  border-radius: 5px;
  background: #fff;
  filter: blur(0.2px);
  opacity: 0.8;
}

.ms-login span {
  color: red;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
}

.code {
  width: 112px;
  height: 35px;
  border: 1px solid #ccc;
  float: right;
  border-radius: 2px;
}

.validate-code {
  width: 136px;
  float: left;
}

.register {
  font-size: 14px;
  line-height: 30px;
  color: #999;
  cursor: pointer;
  float: right;
}
</style>
